package vn.gq.tym.utils;

import vn.gq.tym.tonghopvf.NoNetworkActivity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.support.v4.app.FragmentActivity;

public class NetworkUtils {
	
	private FragmentActivity mContext;
	
	public NetworkUtils(FragmentActivity context) {
		mContext = context;
	}
	
	public boolean hasConnection() {
		ConnectivityManager conmgr = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo activeInfo = conmgr.getActiveNetworkInfo();
		if (activeInfo != null) {
			if (activeInfo.getState() == State.CONNECTED || (activeInfo.getState() == State.CONNECTING)) {
				return true;
			}
		}
		return false;
	}
	
	public void check() {
		ConnectivityManager conmgr = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo activeInfo = conmgr.getActiveNetworkInfo();
		if (activeInfo != null) {
			if (activeInfo.getState() == State.CONNECTED || (activeInfo.getState() == State.CONNECTING)) {
				return;
			}
		}
		mContext.finish();
		Intent intent = new Intent(mContext, NoNetworkActivity.class);
		intent.putExtra("INTENT", mContext.getIntent());
		mContext.startActivity(intent);
	}
	
	public static NetworkUtils newInstance(FragmentActivity context) {
		NetworkUtils nwku = new NetworkUtils(context);
		return nwku;
	}
}
